function change (t) {
  if (t < 10) {
    return '0' + t
  } else {
    return t
  }
}

export function timestampToTime (timestamp, t) {
  let date = new Date(timestamp * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-'
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = change(date.getDate()) + ' '
  let h = change(date.getHours()) + ':'
  let m = change(date.getMinutes()) + ':'
  let s = change(date.getSeconds())
  if (t === 1) {
    return Y + M + D + h + m + s
  } else if (t === 2) {
    return h + m + s
  } else if (t === 3) {
    let gt = h + m
    return gt.substr(0, gt.length - 1)
  } else if (t === 4) {
    return M + D
  }
}

export const haveEmpty = function (obj) {
  if (typeof obj !== 'object') { return false }
  for (let key in obj) {
    if (obj[key] === '') {
      return false
    }
  }
  return true
}

function timestampToTime2 (timestamp) {
  let date = new Date(timestamp) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let h = change(date.getHours()) + ':'
  let m = change(date.getMinutes())
  return h + m
}

export function getTableData (t) {
  let arr = []
  var time = 1548172800000
  for (var i = 0; i < 1440 / t; i++) {
    var obj = {
      time: timestampToTime2(time) + '~' + timestampToTime2(time += t * 60000),
      bidsHigh: '0',
      bidsLow: '0',
      asksHigh: '0',
      asksLow: '0',
      amountHight: '0',
      amountLow: '0',
      depth: '0',
      frequency: '0',
      open: '0',
      close: '0'
    }
    arr.push(obj)
  }
  return arr
}

export function isEmpty (d) {
  console.log(d)
  if (typeof d === 'object') {
    let res = false
    for (let i in d) {
      if (/^ *$/.test(d[i])) {
        return true
      }
    }
    return res
  } else {
    return true
  }
}

export function modelDetailSort (arr) {
  let sortArr = arr
  let a = []
  for (let i = 0; i < sortArr.length; i++) {
    a.push({
      open_rate: sortArr[i].open_rate,
      high_rate: sortArr[i].high_rate,
      low_rate: sortArr[i].low_rate,
      close_rate: sortArr[i].close_rate
    })
  }
  console.log(a)
  return a
}

export function objValToString (a) {
  let arr = []
  for (let i = 0; i < a.length; i++) {
    let o = a[i]
    for (let idx in o) {
      o[idx] += ''
    }
    arr.push(o)
  }
  return arr
}
